<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\Blacklist\CreateRequest;
use App\Models\Common\CcBlacklist;
use Illuminate\Http\Request;
use Style;

class BlacklistController extends CommonController{
    /**
     * 显示黑名单
     * @return mixed
     */
    public function index(){
        $ccBlacklist = CcBlacklist::orderBy('updated_at', 'desc')->paginate(20);
        return Style::view('admin.safety.cc_blacklist.index', compact('ccBlacklist'));
    }

    /**
     * 添加黑名单
     * @return mixed
     */
    public function create(){
        return Style::view('admin.safety.cc_blacklist.create');
    }

    /**
     * 添加黑名单提交
     * @param CreateRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(CreateRequest $request){
        $input = $request->only(['ip']);
        $result = CcBlacklist::create($input);
        if($result){
            flash('添加黑名单成功', 'success');
            return redirect('admin/blacklist');
        }else{
            return back()->with('errors', '添加黑名单失败，请稍后重试！');
        }
    }

    /**
     * @param $id
     */
    public function show($id){
    }

    /**
     * 编辑黑名单
     * @param $id
     * @return mixed
     */
    public function edit($id){
        $ccBlacklist = CcBlacklist::find($id);
        return Style::view('admin.safety.cc_blacklist.edit', compact('ccBlacklist'));
    }

    /**
     * 更新黑名单
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(CreateRequest $request, $id){
        $input = $request->only(['ip']);
        $result = CcBlacklist::where('id', $id)->update($input);
        if($result){
            flash('更新黑名单管理成功', 'success');
            return redirect('admin/blacklist');
        }else{
            return back()->with('errors', '更新前和更新后数据相同，更新失败！！');
        }
    }

    /**
     * 删除黑名单
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id){
        $result = CcBlacklist::where('id', $id)->delete();
        if($result){
            return response()->json(['status' => 0, 'msg' => '删除黑名单管理成功！']);
        }else{
            return response()->json(['status' => 1, 'msg' => '删除黑名单管理失败，请稍后重试！']);
        }
    }
}
